package sc
{
	import flash.desktop.NativeApplication;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.display.NativeWindow;
	import flash.events.Event;
	
	public class Menue
	{
		public function Menue()
		{
			var fileMenu:NativeMenuItem;
            var editMenu:NativeMenuItem;
            
            if (NativeWindow.supportsMenu){
                $.main.stage.nativeWindow.menu = new NativeMenu();
                $.main.stage.nativeWindow.menu.addEventListener(Event.SELECT, selectCommandMenu);
                fileMenu = $.main.stage.nativeWindow.menu.addItem(new NativeMenuItem("Flexy"));
                
                fileMenu.submenu = createFileMenu();
                editMenu = $.main.stage.nativeWindow.menu.addItem(new NativeMenuItem("edit"));
                editMenu.submenu = createEditMenu();
            }
            
            if (NativeApplication.supportsMenu){
                NativeApplication.nativeApplication.menu.addEventListener(Event.SELECT, selectCommandMenu);
                fileMenu = NativeApplication.nativeApplication.menu.addItem(new NativeMenuItem("Flexy"));
                fileMenu.submenu = createFileMenu();
                editMenu = NativeApplication.nativeApplication.menu.addItem(new NativeMenuItem("Edit"));
                editMenu.submenu = createEditMenu();
            }

		}
                
        public function createFileMenu():NativeMenu {
            var fileMenu:NativeMenu = new NativeMenu();
            fileMenu.addEventListener(Event.SELECT, selectCommandMenu);
            
            var newCommand:NativeMenuItem = fileMenu.addItem(new NativeMenuItem("New"));
            newCommand.addEventListener(Event.SELECT, $.file.newDoc);
            newCommand.keyEquivalent = "n";
            var saveCommand:NativeMenuItem = fileMenu.addItem(new NativeMenuItem("Save"));
            saveCommand.addEventListener(Event.SELECT, $.file.saveFile);
            saveCommand.keyEquivalent = "s";
            var openCommand:NativeMenuItem = fileMenu.addItem(new NativeMenuItem("Open..."));
            openCommand.addEventListener(Event.SELECT, $.file.openFile);
            openCommand.keyEquivalent = "o";
            var preferencesCommand:NativeMenuItem = 
                    fileMenu.addItem(new NativeMenuItem("Preferences"));
            preferencesCommand.keyEquivalent = ",";
            preferencesCommand.addEventListener(Event.SELECT, function(event:*):void{$.main.ws.selectedIndex = 1;});
            var publishCommand:NativeMenuItem = fileMenu.addItem(new NativeMenuItem("Publish..."));
            openCommand.addEventListener(Event.SELECT, function(event:*):void{$.main.ws.selectedIndex = 2;});
            openCommand.keyEquivalent = "e";
            
            
            return fileMenu;
        }
        
        public function createEditMenu():NativeMenu {
            var editMenu:NativeMenu = new NativeMenu();
            editMenu.addEventListener(Event.SELECT, selectCommandMenu);
            
            
            var preferencesCommand:NativeMenuItem = 
                    editMenu.addItem(new NativeMenuItem("Preferences"));
            preferencesCommand.addEventListener(Event.SELECT, function(event:*):void{$.main.ws.selectedIndex = 1;});
            
            return editMenu;
        }
        
        
        
        private function selectCommand(event:Event):void {
            trace("Selected command: " + event.target.label);
        }

        private function selectCommandMenu(event:Event):void {
            if (event.currentTarget.parent != null) {
                var menuItem:NativeMenuItem =
                        findItemForMenu(NativeMenu(event.currentTarget));
                if (menuItem != null) {
                    trace("Select event for \"" + 
                            event.target.label + 
                            "\" command handled by menu: " + 
                            menuItem.label);
                }
            } else {
                trace("Select event for \"" + 
                        event.target.label + 
                        "\" command handled by root menu.");
            }
        }
        
        private function findItemForMenu(menu:NativeMenu):NativeMenuItem {
            for each (var item:NativeMenuItem in menu.parent.items) {
                if (item != null) {
                    if (item.submenu == menu) {
                        return item;
                    }
                }
            }
            return null;
        }
	}
}